<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$tabs=array(
	array('op'=>'main','title'=>'经营概况'),
	array('op'=>'kanban','title'=>'数据看板'),
	array('op'=>'creditlog','title'=>'充值消费记录'),
	array('op'=>'sale','title'=>'销售统计'),
	array('op'=>'sale_analysis','title'=>'销售指标'),
	array('op'=>'orderdata','title'=>'订单统计'),
	array('op'=>'goods','title'=>'销售明细'),
	array('op'=>'goods_rank','title'=>'销售排行'),
	array('op'=>'goods_trans','title'=>'销售转化率'),
	array('op'=>'member_cost','title'=>'消费排行'),
	array('op'=>'member_increase','title'=>'增长趋势'),
);
set_html('tabs',$tabs);
$merchid=$_SESSION['merchid'];
if($_RQ['op']=='main'){
	if($_RQ['id']){
		$_SESSION['merchid']=$_RQ['id'];
	}
	$merchid=$_SESSION['merchid'];
	$return['item']=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['item']['orderfee']=pdo_getsum('jy_weishop_order',array('ispay'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'price');
	$return['item']['inpayfee']=pdo_getsum('jy_merch_paylog',array('status'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'money');
	$return['item']['orderfee'] +=$return['item']['inpayfee'];
	$return['item']['ordernum']=pdo_count('jy_weishop_order',array('ispay'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
	$return['item']['goodsnum']=pdo_count('jy_weishop_goods',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
	$return['item']['logo']=tomedia($return['item']['logo']);
	$return['isopenvoice']=$return['item']['isopenvoice'];
	$return['merchs']=pdo_getall('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$return['item']['logo'];
	$this->session('plugin_icon',$return['item']['logo']);
	$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid']),'avatar');
	$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
	$return['item']['url']=custom_url('jy_merch/merch//id='.$merchid,true);
	$return['item']['rechargeurl']=custom_url('jy_merch/member.recharge//merchid='.$merchid,true);
	$return['item']['payurl']=custom_url('jy_merch/inpay//merchid='.$merchid,true);
	if($this->plugin_exists('jy_commission')){
		$jy_commission=1;
	}
	$return['data2']['orderfee']=$return['item']['orderfee'];
	//计算待结算金额
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3,'merchid'=>$merchid,'clearstatus'=>0);
	if($return['pluginset']['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($return['pluginset']['balance_day'])*86400;
		$params['finishtime <']=$time;
	}
	$return['jiesuantotal']=pdo_count('jy_weishop_order',$params);
	$return['params']=array('daytype'=>0,'time'=>'','qushi'=>0,'qushitype'=>0);
	$return['tmplIds']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0);
	$params['status']=1;
	$return['status1']=pdo_count('jy_weishop_order',$params);
	$params['status']=0;
	$return['ispay']=pdo_count('jy_weishop_order',$params);
	$params['status']=2;
	$return['status2']=pdo_count('jy_weishop_order',$params);
	$return['goods']=pdo_count('jy_weishop_goods',array('status <>'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['delgoods']=pdo_count('jy_weishop_goods',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['verifygoodsnum']=pdo_count('jy_weishop_verifygoods_log',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'salerid'=>$_SESSION['uid']),'id');
	$return['verifynum']=pdo_count('jy_weishop_order_verify',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['selfverifynum']=pdo_count('jy_weishop_order_self',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['jy_areashop'] = $this->plugin_exists('jy_areashop');
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
	$return['ideal_money']=pdo_getcolumn('core_ideal_money',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$credit_types=credit_types();
	$credit_types2=array();
	foreach($credit_types as $key=>$value){
		$fee=$return['member']['credit'.$key];
		$credit_types2[$key]=array(
			'id'=>$key,
			'title'=>$value,
			'fee'=>$fee
		);
	}
	$return['credit_types']=$credit_types2;
	//判断商户独立提现
	$setting=$this->plugin_setting('setting');
	foreach($setting['cash_set'] as $cv){
		if($cv['rate'] && $cv['type']){
			$return['merchtixian']=1;
		}
	}
	set_html('title','商户后台');
	exi($return);
}
if($_RQ['op']=='getdata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	//创建时间
	if($_RQ['tstart'] && $_RQ['tend']){
		$params['createtime between']=array($_RQ['tstart'],$_RQ['tend']);
	}else{
		$daytype=intval($_RQ['daytype']);
		if($daytype==0){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime >']=$daytime;
		}elseif($daytype==1){
			$yesdaytime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime between']=array($yesdaytime,$daytime);
		}elseif($daytype==2){
			$day7time = mktime(0,0,0,date('m'),date('d')-7,date('Y'));
			$params['createtime >']=$day7time;
		}elseif($daytype==3){
			
		}
	}
	//print_r($params);exit;
	$params['ispay']=1;
	$data['paybi']=pdo_count('jy_weishop_order',$params);
	$data['payren']=pdo_count('jy_weishop_order',$params);
	$data['payyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['payjian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$data['shoubi']=pdo_count('jy_weishop_order',$params);
	$data['shouren']=pdo_count('jy_weishop_order',$params);
	$data['shouyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['shoujian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$return['params']=$params;
	if($_SESSION['os']=='windows'){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$access['status_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
		$access['status_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid']));
		$access['status1_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'createtime >'=>$daytime));
		$access['status1_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3)));
		//月份
		for($i=0;$i<12;$i++){
					if($i==0){
						$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
						$yueend = time();
						
					}else{
						$yuestart = mktime(0,0,0,date('m')-$i,1,date('Y'));
						$yueend = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
					}
					$params=array('uniacid'=>$_SESSION['uniacid']);
					$params['createtime between']=array($yuestart,$yueend);
					$month12[$i] = date('Y-m',strtotime('-'.$i.' month'));
					//$monthaccess[]=pdo_count('core_access_log',$params);
					$accessline[$i]=pdo_count('jy_weishop_order',$params);
					//$monthcredit2[]=pdo_getsum('core_members',$params,'credit2');
		}
		$accessline=array_reverse($accessline);
		$month12=array_reverse($month12);
		$htmls=array(
					array('type'=>'data','tpltype'=>4,'one_num'=>4,'title'=>'今日/总订单数据指标','datas'=>array(
						array('title'=>'今日/总订单数','num'=>2,'data'=>array($access['status_day'],$access['status_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总成交数','bgcolor'=>'#009933','num'=>2,'data'=>array($access['status1_day'],$access['status1_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'待发货/收货数','bgcolor'=>'#3366FF','num'=>2,'data'=>array($return['status1'],$return['status2']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总订单数','bgcolor'=>'#FF3300','num'=>2,'data'=>array($access['pc_day'],$access['status_all']),'datatitle'=>array('客户数','申请数')),
					)),
					array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'月度订单数据','datas'=>array(
						'datas'=>array(
							array('name'=>'订单数','value'=>$accessline),
						),
						'xdatas'=>$month12,
					)),
		);	
		//$return['htmls']=$htmls;
	}
	$return['data']=$data;
	exi($return);
}
if($_RQ['op']=='kanban'){
	//显示统计数据
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$mounthtime =  mktime(0,0,0,date('m'),1,date('Y'));
	$yeartime=strtotime(date('Y-m-d',mktime(0, 0 , 0,1,1,date("Y"))));
	$data['monthfee']=pdo_getsum('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$mounthtime),'price');
	$data['monthorder']=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$mounthtime));
	$data['yearfee']=pdo_getsum('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$yeartime),'price');
	$data['yearnums']=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$yeartime));
	$data['yearmembers']=pdo_getnorepeat('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$yeartime),'mid');
	$data['yearmembers']=count($data['yearmembers']);
	//7日销量走势图
	$month12=array();
	for($i=0;$i<7;$i++){
			if($i==0){
				$dayend = $daytime;
				$daystart = $dayend-24*3600;
			}else{
				$dayend = $daytime-$i*24*3600;
				$daystart = $dayend-24*3600;
			}
			$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3,5,6));
			$params['createtime between']=array($daystart,$dayend);
			$month12[] = date('m-d',$daystart);
			$monthorder[]=pdo_count('jy_weishop_order',$params);
			$monthfee=pdo_getsum('jy_weishop_order',$params,'price');
			$monthmoney[]=intval($monthfee);
	}
	$monthorder=array_reverse($monthorder);
	$monthmoney=array_reverse($monthmoney);
	$month12=array_reverse($month12);
	$return['data01']=array(
		'datas'=>array(
			array('name'=>'成交订单数','value'=>$monthorder),
			array('name'=>'成交订单金额','value'=>$monthmoney),
		),
		'xdatas'=>$month12,
	);
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$brands=pdo_getall('jy_weishop_goods',$params,array('id','title'));
	foreach($brands as $k=>&$v){
		$v['total']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid'=>$v['id'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$mounthtime),'total');
	}
	paixu($brands,'total','desc');
	$values=$names=array();
	$i=1;
	foreach($brands as $v){
		$i ++;
		$names[]=$v['title'];
		$values[]=$v['total'];
		if($i>=5){
			break;
		}
	}
	$return['data03']=array(
		'names'=>$names,
		'values'=>$values,
	);
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$levels=pdo_getall('jy_weishop_member_level',$params,array('id','title'));
	$levels[]=array('id'=>0,'title'=>'默认等级');
	foreach($levels as $k=>&$v){
		$mids=pdo_getall('jy_weishop_member',array('merchid'=>$merchid,'level'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		$v['total']=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$mids,'ispay'=>1,'createtime >'=>$mounthtime));
		$mids=pdo_getall('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$mounthtime),'mid','mid');
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		$v['total2']=pdo_count('jy_weishop_member',array('merchid'=>$merchid,'level'=>$v['id'],'mid'=>$mids,'uniacid'=>$_SESSION['uniacid']));
	}
	paixu($levels,'total','desc');
	$values=$names=array();
	$orderuser=$members=0;
	foreach($levels as $nv){
		$names[]=$nv['title'];
		$values[]=$nv['total'];
		$values2[]=$nv['total2'];
		$orderuser +=$nv['total2'];
		if($nv['id']){
			$members +=$nv['total2'];
		}
	}
	$return['data04']=array(
		'names'=>$names,
		'values1'=>$values,
		'values2'=>$values2,
	);
	$allmembers=pdo_count('jy_weishop_member',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$mounthtime));
	$ordernum=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'createtime >'=>$mounthtime));
	$return['data02']=array(
		'names'=>array('访客','成交客户数','订单数','等级客户数'),
		'values'=>array($allmembers,$orderuser,$ordernum,$members),
	);
	$return['data']=$data;
	$return['shopset']=$this->plugin_setting('shopset');
	$return['logo']=$return['shopset']['logo']?:'../addons/jy_weishop/resource/images/logo.png';
	exi($return);
}
if($_RQ['op']=='creditlog'){
	$return['adds']=array(
		'0'=>'消费',
		'1'=>'获得',
		'2'=>'提现',
	);
	$return['types2']=array(
			array('id'=>1,'title'=>'积分'),
			array('id'=>2,'title'=>'余额'),
	);
	$return['adds2']=array(
			array('id'=>0,'title'=>'消费'),
			array('id'=>1,'title'=>'获得'),
			array('id'=>2,'title'=>'提现'),
	);
	$return['types']=credit_types(true);
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$return['params']=array();
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//日志类型，1积分，2余额
	if(!empty($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	//日志种类，1增加，0减少
	if(isset($_RQ['is_add'])){
		$params['is_add']=intval($_RQ['is_add']);
		$return['params']['is_add']=$_RQ['is_add'];
	}
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('jy_merch_creditlog',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'会员id','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'type','title'=>'积分类型','type'=>'radio','options'=>'types'),
			array('name'=>'is_add','title'=>'日志类型','type'=>'radio','options'=>'adds'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'title'=>'积分类型','name'=>'type','options'=>'types'),
			array('type'=>'select','width'=>15,'title'=>'日志类型','name'=>'is_add','options'=>'adds2'),
			array('type'=>'choose','width'=>15,'title'=>'会员id','name'=>'mid','action'=>'member.get_member','choose_title'=>'选择会员'),
			array('type'=>'time_range','width'=>18,'title'=>'创建时间','name'=>'createtime','help'=>''),
		),
	);
	exi($return);
}
if($_RQ['op']=='sale'){
	$params=array('merchid'=>$merchid,'ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_weishop_order',$params,'price');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'fee','title'=>'销售额(元)(已支付)','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='orderdata'){
	$return['list']['0']['totalprice']=pdo_getsum('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0),'price');
	$return['list']['0']['total']=pdo_count('jy_weishop_order',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0));
	$return['list']['0']['totalmember']=pdo_count('jy_weishop_member',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$m=pdo_fetch('select count(1) from '.tablename('jy_weishop_order').' where  uniacid = '.$_SESSION['uniacid'].' and merchid = '.$merchid.' and ispay=1 and deleted=0 group by mid');
	$return['list']['0']['xfmember']=$m['count(1)']?$m['count(1)']:0;
	if($return['list']['0']['totalmember']>0){
		$return['list']['0']['buy']=price($return['list']['0']['xfmember']/$return['list']['0']['totalmember']*100);		
		$return['list']['0']['xf']=price($return['list']['0']['total']/$return['list']['0']['totalmember']*100);
	}
	$htmls=array(
		'type'=>'list',	
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'totalprice','title'=>'订单总金额(已支付)','type'=>'string'),
			array('name'=>'total','title'=>'总订单数(已支付)','type'=>'string'),			
			array('name'=>'totalmember','title'=>'总会员数','type'=>'string'),
			array('name'=>'xfmember','title'=>'消费会员数','type'=>'string'),
			array('name'=>'buy','title'=>'会员消费率(%)','type'=>'string'),
			array('name'=>'xf','title'=>'订单购买率(%)','type'=>'string'),
		)),
		
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='sale_analysis'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	if(isset($_RQ['send_status'])){
		$params['send_status']=intval($_RQ['send_status']);
		$return['params']['send_status']=$_RQ['send_status'];
	}
	if(isset($_RQ['ispay'])){
		$params['ispay']=intval($_RQ['ispay']);
		$return['params']['ispay']=$_RQ['ispay'];
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if(isset($_RQ['merchid'])){
		$params['merchid']=intval($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','ordersn','address','realname','mobile','mid','paytype','api_data','createtime');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$address=json_decode($item['address'],true);
			$realname=$address['name']?:$item['realname'];
			$mobile=$address['mobile']?:$item['mobile'];
			$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id']),array('price','total','goodsid'));
			$item['total']=0;
			$item['api_data']=json_decode($item['api_data'],true);
			foreach($goods as &$v){
				$item['total'] +=$v['total'];
				$good=pdo_get('jy_weishop_goods',array('id'=>$v['goodsid']),array('title','image'));
				$return['list'][]=array(
					'ordersn'=>$item['ordersn'],
					'createtime'=>date('Y-m-d H:i:s',$item['createtime']),
					'image'=>$good['image'],
					'title'=>$good['title'],
					'price'=>$v['price'],
					'total'=>$v['total'],
					'dian_id'=>$item['api_data']['area_dianid'],
					'realname'=>$realname,
					'mobile'=>$mobile,
				);
			}
		}
	}
	//$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'total'=>$total,'psize'=>$psize,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'price','title'=>'单价','type'=>'string'),
			array('name'=>'total','title'=>'商品数量','type'=>'string'),
			array('name'=>'realname','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'string'),
		)),'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'ispay','width'=>10,'title'=>'支付状态','type'=>'radio','options'=>'ispay'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型_Type','type'=>'radio','options'=>'buytype'),
			array('name'=>'sendtype','width'=>10,'title'=>'配送方式_Ship type','type'=>'radio','options'=>'sendtype'),
			array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
			array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
		)
	);
	$return['ispay']=array('未支付','已支付');
	$return['buytype']=$this->buy_types;
	$return['sendtype']=$this->send_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='goods'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'status'=>3);	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','price','ordersn','finishtime','paytime','createtime');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		$goodslist=array();
		foreach($list as $k=>&$v){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','total','price'));
			$v['total']=0;
			foreach($ordergoods as &$gv){
				$goods=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('title','image'));
				$gv['title']=$goods['title'];
				$gv['image']=$goods['image'];
				$gv['ordersn']=$v['ordersn'];
				$v['paytime']=$v['paytime']?:$v['createtime'];
				$gv['paytime']=date('Y-m-d H:i:s',$v['paytime']);
				$goodslist[] = $gv;
				$v['total'] +=$gv['total'];
			}
		}
	}
	$return['list']=$goodslist;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单编号','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'price','title'=>'单价','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'paytime','title'=>'成交时间','type'=>'string'),
		)),
	
	);

	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='goods_rank'){
	$params=array('type <>'=>4,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$params['onepluginname']='';
	$return['list']=pdo_getall('jy_weishop_goods',$params,array('id','title','image'));
	foreach($return['list'] as $k=>&$v){
		$v['total']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		if(!$v['total']){
			unset($return['list'][$k]);
			continue;
		}
		$v['totalprice']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
	}
	paixu($return['list'],'total','desc');
	foreach($return['list'] as $k=>&$v){
		$v['rank']=$k+1;
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排行','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),			
			array('name'=>'total','title'=>'销售量','type'=>'string'),
			array('name'=>'totalprice','title'=>'销售额','type'=>'string'),
		)),
	
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='goods_trans'){
	$params=array('type <>'=>4,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$params['onepluginname']='';
	$return['list']=pdo_getall('jy_weishop_goods',$params,array('id','title','image','readnum'));
	foreach($return['list'] as $k=>&$v){
		$v['total']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'goodsid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'total');
		if(!$v['total']){
			unset($return['list'][$k]);
			continue;
		}
		$v['readnum']=$v['readnum']?:$v['total'];
		$v['rate']=price($v['total']/$v['readnum']*100);
	}
	paixu($return['list'],'rate','desc');
	foreach($return['list'] as $k=>&$v){
		$v['rank']=$k+1;
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排行','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),	
			array('name'=>'readnum','title'=>'访问量','type'=>'string'),			
			array('name'=>'total','title'=>'销售量','type'=>'string'),
			array('name'=>'rate','title'=>'转换率(%)','type'=>'string'),
			
		)),
	
	);

	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='member_cost'){
	$pmrq=$y.'.'.$m.'.'.$d;
    $d2=$d+1;
    $starttime=strtotime($y.'-'.$m.'-'.$d);
    $endtime=strtotime($y.'-'.$m.'-'.$d2);
    $search=array(
        array('name'=>'y','width'=>15,'title'=>'年','type'=>'radio','options'=>'ys'),
        array('name'=>'m','width'=>15,'title'=>'月','type'=>'radio','options'=>'months'),
        array('name'=>'d','width'=>15,'title'=>'日','type'=>'radio','options'=>'days'),
    );
	$return['list']=pdo_fetchall('SELECT mid,SUM(price),count(1) FROM '.tablename('jy_weishop_order').' WHERE ispay=1 and merchid= '.$merchid.$params.' group by mid');
	foreach($return['list'] as $k=>&$v){
		$member=pdo_get('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','nickname','mobile'));
		$member2=pdo_get('jy_weishop_member',array('merchid'=>$merchid,'mid'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),'level');
		$member3=pdo_get('jy_weishop_member_level',array('merchid'=>$merchid,'id'=>$member2['level']),'title');
		if(!$member['id']){
			unset($return['list'][$k]);
			continue;
		}
		$v['nickid']=$v['mid'];
		$v['nickname']=$member['nickname'];
		$v['level']=$member3['title'];
		$v['mobile']=$member['mobile'];
		$v['total']=$v['count(1)'];
		$v['totalprice']=$v['SUM(price)'];
	}
	paixu($return['list'],'SUM(price)','desc');
	foreach($return['list'] as $k=>&$v){
		$v['rank']=$k+1;
	}
	$htmls=array(
    'type'=>'list',
    'tabs'=>$tabs,
    'search'=>$search,
    'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
        array('name'=>'rank','title'=>$pmrq.' 排名','type'=>'string'),
        array('name'=>'nickid','title'=>'会员ID','type'=>'string'),
        array('name'=>'nickname','title'=>'会员昵称','type'=>'string'),
        array('name'=>'level','title'=>'商城等级','type'=>'string'),
        array('name'=>'mobile','title'=>'手机','type'=>'string'),         
        array('name'=>'totalprice','title'=>'消费金额','type'=>'string'),
        array('name'=>'total','title'=>'订单数','type'=>'string'),
    )),
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年' ,
	);
	for($i=1;$i<=31;$i++){
		$days[$i]=$i.'日';
		if($i<=12){$months[$i]=$i.'月';}
	}
	$return['months']=$months;
	$return['days']=$days;
	exi($return);
}
if($_RQ['op']=='member_increase'){
	$return['list']=pdo_fetchall('SELECT mid,SUM(price),count(1) FROM '.tablename('jy_weishop_order').' WHERE ispay=1 and merchid= '.$merchid.' group by mid');
	foreach($return['list'] as $k=>&$v){
		$member=pdo_get('jy_merch_member',array('mid'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','name','mobile'));
		if(!$member['id']){
			unset($return['list'][$k]);
			continue;
		}
		$v['nickname']=$member['name'];
		$v['mobile']=$member['mobile'];
		$v['rank']=$k+1;
		$v['total']=$v['count(1)'];
		$v['totalprice']=$v['SUM(price)'];
	}
	paixu($return['list'],'SUM(price)','desc');
	foreach($return['list'] as $k=>&$v){
		$v['rank']=$k+1;
	}
	$htmls=array(
		'type'=>'list',
		
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'rank','title'=>'排行','type'=>'string'),
			array('name'=>'nickname','title'=>'会员昵称','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),			
			array('name'=>'totalprice','title'=>'消费金额','type'=>'string'),
			array('name'=>'total','title'=>'订单数','type'=>'string'),
			
		)),
	
	);

	$return['htmls']=$htmls;
	
	exi($return);
}